package one.stand.service.impl;

import one.stand.code.RedisCode;
import one.stand.config.GlobalConfig;
import one.stand.enums.CompanyTypeEnums;
import one.stand.enums.GlobalTypeEnums;
import one.stand.model.BaseModel;
import one.stand.model.CompanyAccountModel;
import one.stand.model.GlobalConfigurationModel;
import one.stand.request.CompanyAccountRequest;
import one.stand.service.CompanyAccontService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: QIK
 * @CreateDate: 2019/4/29 11:25
 */
@Service
@Transactional
public class CompanyAccontServiceImpl extends PublicBaseService implements CompanyAccontService {
    @Autowired
    GlobalConfig globalConfig;
    @Override
    @Cacheable(value = RedisCode.REDIS_DB_CACHE,key = "#root.targetClass.toString()+'.'+#root.methodName+':'+#request.toString()",unless = "#result==null")
    public CompanyAccountModel selectByCompanyKey(CompanyAccountRequest request) {
        CompanyAccountModel inModel=new CompanyAccountModel();
        BeanUtils.copyProperties(request,inModel);
        inModel.setIsDel(BaseModel.NO_DEL);
        return companyAccountMapper.selectByCompanyKey(inModel);
    }

    @Override
    @Cacheable(value = RedisCode.REDIS_DB_CACHE,key = "#root.targetClass.toString()+'.'+#root.methodName+':'+#request.toString()",unless = "#result==null")
    public CompanyAccountModel selectByCompanyKeyDefault(CompanyAccountRequest request) {
        CompanyAccountModel inModel=new CompanyAccountModel();
        BeanUtils.copyProperties(request,inModel);
        inModel.setIsDel(BaseModel.NO_DEL);
        inModel=companyAccountMapper.selectByCompanyKey(inModel);
        return defaultAccount(inModel,request.getAccountType());
    }

    @Override
    @Cacheable(value = RedisCode.REDIS_DB_CACHE,key = "#root.targetClass.toString()+'.'+#root.methodName+':'+#request.toString()",unless = "#result==null")
    public CompanyAccountModel selectByUserId(CompanyAccountRequest request) {
        CompanyAccountModel inModel=new CompanyAccountModel();
        BeanUtils.copyProperties(request,inModel);
        inModel.setIsDel(BaseModel.NO_DEL);
        inModel = companyAccountMapper.selectByUserId(inModel);
        return defaultAccount(inModel,request.getAccountType());
    }

    @Override
    @Cacheable(value = RedisCode.REDIS_DB_CACHE,key = "#root.targetClass.toString()+'.'+#root.methodName+':'+#request.toString()",unless = "#result==null")
    public CompanyAccountModel selectByUserIdCopy(CompanyAccountRequest request) {
        CompanyAccountModel inModel=new CompanyAccountModel();
        BeanUtils.copyProperties(request,inModel);
        inModel.setIsDel(BaseModel.NO_DEL);
        inModel=companyAccountMapper.selectByUserIdCopy(inModel);
        return defaultAccount(inModel,request.getAccountType());
    }

    /**
     * 获取默认账号
     * @param inModel
     * @param type
     * @return
     */
    public CompanyAccountModel defaultAccount( CompanyAccountModel inModel,Integer type){
        if(null==inModel|| CompanyTypeEnums.SERVICE.getCode().equals(inModel.getCompanyType())){
            GlobalConfigurationModel configurationModel= globalConfigurationMapper.select(new GlobalConfigurationModel(){{
                setGlobalKey(globalConfig.getGlobal_default_company_key());
                setGlobalType(GlobalTypeEnums.SERVICE.getCode());
            }});
            inModel = selectByCompanyKey(new CompanyAccountRequest() {{
                setCompanyKey(configurationModel.getGlobalValue());
                setAccountType(type);
            }});
        }
        return inModel;
    }



    @Override
    @Cacheable(value = RedisCode.REDIS_DB_CACHE,key = "#root.targetClass.toString()+'.'+#root.methodName+':'+#request.toString()",unless = "#result==null")
    public CompanyAccountModel selectByOrdersId(CompanyAccountRequest request) {
        CompanyAccountModel inModel=new CompanyAccountModel();
        BeanUtils.copyProperties(request,inModel);
        inModel.setIsDel(BaseModel.NO_DEL);
        inModel=companyAccountMapper.selectByOrdersId(inModel);
        return defaultAccount(inModel,request.getAccountType());
    }

    @Override
    @Cacheable(value = RedisCode.REDIS_DB_CACHE,key = "#root.targetClass.toString()+'.'+#root.methodName+':'+#request.toString()",unless = "#result==null")
    public CompanyAccountModel selectByOrdersIdCopy(CompanyAccountRequest request) {
        CompanyAccountModel inModel=new CompanyAccountModel();
        BeanUtils.copyProperties(request,inModel);
        inModel.setIsDel(BaseModel.NO_DEL);
        inModel= companyAccountMapper.selectByUserIdCopy(inModel);
        return defaultAccount(inModel,request.getAccountType());
    }
}
